﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DTO;
using BUS;

namespace TVC_WEB.Global
{
    public partial class NewsList : System.Web.UI.Page
    {
        string strHRef = "";
        public static int CurrentPage = 0;
        int _IDNewsCategory = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            loadPage();
        }

        private void loadPage()
        {
            PagedDataSource objPsrc = new PagedDataSource();

            string sErr = "";
            if (Request["IDNewsCategory"] != null)
            {
                Session["IDNewsCategory"] = Request["IDNewsCategory"].ToString();
                //     string[] siteDetails = ConfigurationSettings.AppSettings["SiteDetail"].ToString().Split(',');
                //     int idSiteDetail = -1;
                _IDNewsCategory = Convert.ToInt32(Request["IDNewsCategory"]);
                //for (int i = 0; i < siteDetails.Length; i++)
                //{
                //    if (_IDNewsCategory == Convert.ToInt32(siteDetails[i]))
                //    {
                //        idSiteDetail = _IDNewsCategory;
                //        break;
                //    }
                //}

                NewsControl newsControl = new NewsControl();
                DataTable dtb = newsControl.GetAll_News(_IDNewsCategory, 1, ref sErr);


                if (dtb.Rows.Count == 1)
                {
                    //Session["newid"] = dtb.Rows[0]["ID"];
                    //Response.Redirect("~/NewsDetail.aspx");
                    NewsCategoriesControl newsCateControl = new NewsCategoriesControl();
                    NewsCategoriesInfo newsCate = newsCateControl.Get(_IDNewsCategory, ref sErr);

                    lbCategoryNew.Text = newsCate.NameVN;
                    getNewDetail(Convert.ToInt32(dtb.Rows[0]["ID"]));
                    getNewCategory(_IDNewsCategory, Convert.ToInt32(dtb.Rows[0]["ID"]));

                    listnews.Attributes["style"] = "display:none;";
                    listdetailnews.Attributes["style"] = "display:inherit;";
                    return;
                }
                else
                {
                    listnews.Attributes["style"] = "display:inherit;";
                    listdetailnews.Attributes["style"] = "display:none;";
                }

                objPsrc.DataSource = dtb.DefaultView;
                objPsrc.AllowPaging = true;
                objPsrc.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["NumberNews"].ToString());
                strHRef = "Group";
                objPsrc.CurrentPageIndex = CurrentPage;
                if (Request.Params["page"] != null)
                {
                    CurrentPage = Convert.ToInt32(Request.Params["page"].ToString()) - 1;
                    objPsrc.CurrentPageIndex = CurrentPage;
                }
                lstTinTuc.DataSource = objPsrc;
                lstTinTuc.DataBind();
                this.SetPager(tblPager, dtb, CurrentPage);

                btnNext.Enabled = !objPsrc.IsLastPage;
                btnPrev.Enabled = !objPsrc.IsFirstPage;

                NewsCategoriesControl newsCateControl1 = new NewsCategoriesControl();
                NewsCategoriesInfo newsCate1 = newsCateControl1.Get(_IDNewsCategory, ref sErr);
                lbcatenews.Text = newsCate1.NameVN;
                //DataTable dt = newsCateControl.ge
            }
        }
        NewsControl newsctr = new NewsControl();
        NewsInfo newsinfo = new NewsInfo();
        string sErr = "";

        public void getNewDetail(long iID)
        {
            newsinfo = newsctr.Get(iID, ref sErr);
            DataTable dt = newsinfo.ToDataTable();
            dt.Rows.Add(newsinfo.ToDataRow(dt));
            listdetailnew.DataSource = dt;
            listdetailnew.DataBind();
        }
        public void getNewCategory(int icategoryid, long iID)
        {
            DataTable dt = newsctr.GetAll_News_IDcategory(icategoryid, iID, ref sErr);

            DataTable kq = newsinfo.ToDataTable();
            for (int i = dt.Rows.Count - 1; i >= dt.Rows.Count - 4; i--)
            {
                if (i > -1)
                {

                    NewsInfo temp = new NewsInfo(dt.Rows[i]);

                    kq.Rows.Add(temp.ToDataRow(kq));
                }
            }
            listListNews.DataSource = kq;
            listListNews.DataBind();
        }
        public void SetPager(HtmlTable theTable, DataTable dtb, int iCurrentPage)
        {
            if (dtb != null)
            {
                int iStep = Convert.ToInt32(ConfigurationManager.AppSettings["NumberNews"].ToString());
                theTable.Rows[0].Cells.Clear();
                for (int i = 1; i <= Math.Ceiling(Convert.ToDouble(dtb.Rows.Count) / Convert.ToDouble(iStep)); i++)
                {
                    if (iCurrentPage + 1 != i)
                    {
                        HtmlTableCell cell = new HtmlTableCell();
                        HtmlLink link = new HtmlLink();
                        HtmlAnchor anchor = new HtmlAnchor();
                        anchor.HRef = "NewsList.aspx?IDNewsCategory=" + _IDNewsCategory.ToString() + "&page=" + i.ToString();
                        anchor.InnerText = i.ToString();
                        cell.Controls.Add(anchor);
                        theTable.Rows[0].Cells.Add(cell);
                    }
                    else
                    {
                        HtmlTableCell cell = new HtmlTableCell();
                        cell.InnerText = i.ToString();
                        theTable.Rows[0].Cells.Add(cell);
                    }
                }
            }
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            // CurrentPage = Convert.ToInt32(Request.Params["page"].ToString());

            CurrentPage -= 1;
            // Request.Params["page"]= CurrentPage;
            loadPage();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            // CurrentPage = Convert.ToInt32(Request.Params["page"].ToString());
            CurrentPage += 1;
            // Request.Params["page"] = CurrentPage;
            loadPage();
        }

        public string Formatdatepost(String date)
        {
            String url = Request.Url.PathAndQuery;
            String[] ss = url.Split(new char[] { '/' });
            if (ss[1] == "NewsList.aspx?IDNewsCategory=8")
            {
                String[] s = date.Split('/');
                String kq = "";
                if (s.Length > 0)
                {
                    kq = s[1] + "/" + s[0] + "/" + s[2];
                }
                else
                    kq = "";
                return "Ngày đăng : " + kq;
            }
            return "";
        }
        public string Formatdatepost(String date, String IDcate)
        {

            if (IDcate != "9" && IDcate != "13")
            {
                String[] s = date.Split('/');
                String kq = "";
                if (s.Length > 0)
                {
                    kq = s[1] + "/" + s[0] + "/" + s[2];
                }
                else
                    kq = "";
                return "Ngày đăng : " + kq;
            }
            return "";
        }
    }
}
